package main

import (
	"fmt"
	"os"
	"net"
)

func main() {
	listener,_ := net.Listen("tcp", "127.0.0.1:3000")
	fmt.Println("server listened")
	for {
		conn,_ := listener.Accept()
		fmt.Println("got connection from", conn.RemoteAddr())
		go processConn(conn)
	}

	defer func(){
		listener.Close()
		fmt.Println("server closed")
	}()
}

func processConn(conn net.Conn) {
	defer conn.Close()

	buffer:=make([]byte, 256)
	for {
		n,err:=conn.Read(buffer)
		if n>0 {
			os.Stdout.Write(buffer[:n])
		}
		if err!=nil {
			fmt.Errorf("\nError: %v\n", err)
			break
		}
	}
}
